def drop_null(df):
    '''
    丢弃null,how有'any'&'all',subset取子集
    dropna(how='any', thresh=None, subset=None)
    '''
    df = df.na.drop()
    return df

def fill_null(df):
    '''
    用固定值填充
    fill(value, subset=None)
    '''
    df = df.na.fill(50)   #以50替换所有null值
    df = df.na.fill({'age': 50, 'name': 'unknown'})
    return df

def replace_null(df):
    '''
    替换
    replace(to_replace, value, subset=None)
    '''
    df = df.na.replace(10, 20)
    df = df.na.replace(['Alice', 'Bob'], ['A', 'B'], 'name')
    df = df.select(when(df['age'] == 2, 3).otherwise(4).alias("age"))
    df.select(when(df.age == 2, df.age + 1).alias("age"))
    return df

#将字段值为null用众数代替
def replaceNULL(df, colList):
    for colName in colList:
        modeList = df.groupby(colName).count().orderBy(desc("count")).take(2)
        mode = modeList[0][0]
        if mode == None or mode == "":
            mode = modeList[1][0]
        df = df.na.fill(mode, [colName])
    return df
